<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="small" :inline="true" class="demo-form-inline">
        <el-form-item label="用户名">
          <el-input v-model="params.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="params.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="params.sex" placeholder="请选择">
            <el-option label="男" value="0" />
            <el-option label="女" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="serach">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="addvip('add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportExcel">导出</el-button>
        </div>
        <div>
          <el-tooltip class="item" effect="dark" content="关闭搜索区" placement="top">
            <el-button :icon="up" size="small" circle @click="hide" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button icon="el-icon-refresh" size="small" circle @click="reload" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="筛选" placement="top-start">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkList">
                <el-checkbox v-for="(item,index) in renderList" :key="index" class="fa fa fa-dedent" :label="item.name" />
              </el-checkbox-group>
              <el-button slot="reference" class="filter" icon="el-icon-menu" size="small" circle />
            </el-popover>
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <el-dialog ref="tagdialog" :title="dialog" :visible.sync="dialogVisible" width="45%" @close="handleClose">
      <el-form ref="vipform" :model="vipform" label-width="100px" class="demo-dynamic" size="medium">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="username" label="用户名" :rules="{ required: true, message: '请输入用户名', trigger: 'blur' }">
              <el-input v-model="vipform.username" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="mobile" label="手机号" :rules="{ required: true, message: '请输入手机号', trigger: 'blur' }">
              <el-input v-model="vipform.mobile" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="password" label="密码">
              <el-input v-model="vipform.password" :disabled="dialog==='编辑用户'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="sex" label="性别" :rules="{ required: true, message: '请输入密码', trigger: 'blur' }">
              <el-switch v-model="vipform.sex" class="sexswitch" style="display: block" :active-value="0" :inactive-value="1" active-text="男" inactive-text="女" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item prop="icon" label="用户头像" :rules="{ required: true, message: '请输入用户头像', trigger: 'change' }">
          <el-row>
            <el-col :span="15"><el-upload
              class="upload-demo"
              action="http://124.223.14.236:8060/admin/common/upload?type=images"
              :limit="1"
              :headers="headers"
              :on-success="fileSuccess"
              :file-list="fileList"
              :on-remove="fileremove"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload></el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table id="outTable" ref="myTable" :data="viplist" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50" />
        <el-table-column v-if="checkList.includes('用户ID')" prop="id" label="用户ID" />
        <el-table-column v-if="checkList.includes('用户名')" prop="username" label="用户名" />
        <el-table-column v-if="checkList.includes('手机号')" prop="mobile" label="手机号" />
        <el-table-column v-if="checkList.includes('用户头像')" prop="" label="用户头像">
          <template #default="{row}">
            <el-image style="width: 100px; height: 100px" :src="'http://124.223.14.236:8060/'+row.icon" :fit="'cover'" />
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('性别')" prop="" label="性别">
          <template #default="{row}">
            <div class="status" :class="{'green':row.sex}">{{ row.sex?'女':'男' }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('出生年月')" prop="birthday" label="出生年月" />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="addvip(row.id)" />
            <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
          </template>

        </el-table-column></el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        v-if="isTableAlive"
        background
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { getvip, delvip, addvip, editvip, vipdetail } from '@/api/vip'
import { exportTableAsXLSX } from '@/utils/unitools'
export default {
  data() {
    return {
      dialogVisible: false,
      fileList: [],
      dialog: '添加广告图',
      currentid: '',
      checkList: ['用户ID', '用户名', '手机号', '用户头像', '性别', '出生年月'],
      renderList: [{ name: '用户ID', prop: 'cateid' }, { name: '用户名', prop: 'title' }, { name: '手机号', prop: 'catename' }, { name: '用户头像', prop: 'pic' }, { name: '性别', prop: 'create_date' }, { name: '出生年月', prop: 'ishot' }],
      viplist: [],
      vipform: {
        icon: '',
        mobile: '',
        password: '',
        sex: '',
        username: ''
      },
      params: {
        sex: '',
        username: '',
        mobile: '',
        page: 1,
        limit: 10
      },
      flag: true,
      total: 0,
      up: 'el-icon-arrow-up',
      // 分页显隐
      isTableAlive: true
    }
  },
  computed: {
    headers() {
      return {
        'X-Token': this.$store.getters.token
      }
    }
  },
  async created() {
    this.getviplist()
  },
  methods: {
    submitForm() {
      this.$refs.vipform.validate(
        async(valid) => {
          if (!valid) return
          if (this.dialog === '添加用户') {
            const res = await addvip(this.vipform)
            console.log(res)
            if (res === '') {
              this.$message.success('添加成功')
              this.dialogVisible = false
              this.getviplist()
            }
          } else {
            const res = await editvip({ ...this.vipform, id: this.currentid })
            console.log(res)
            if (res === '') {
              this.$message.success('修改成功')
              this.handleClose()
              this.getviplist()
            }
          }
        }
      )
    },
    handleClose() {
      this.dialogVisible = false
      this.fileList = []
      this.$refs.vipform.resetFields()
    },
    async addvip(id) {
      if (id === 'add') {
        this.dialog = '添加用户'
        // console.log(1)
        this.dialogVisible = true
      } else {
        this.dialog = '编辑用户'
        this.currentid = id
        const res = await vipdetail(id)
        console.log(res)
        this.vipform = res
        this.fileList = [{
          url: 'http://124.223.14.236:8060/' + res.icon
        }]
        this.dialogVisible = true
      }
    },
    fileSuccess(response, file, fileList) {
      console.log(response.data.savePath)
      this.fileList = fileList
      console.log(this.fileList)
      this.vipform.icon = response.data.savePath
      this.$refs.vipform.validate()
    },
    fileremove(file, fileList) {
      console.log(fileList, file)
      this.fileList = fileList
      this.$refs.vipform.validate()
    },
    dodelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delvip(id)
        console.log(res)
        if (res === '') {
          this.$message.success('删除成功')
          this.getviplist()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    exportExcel() {
      exportTableAsXLSX('outTable')
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params.page = 1
      this.params.limit = val
      this.getviplist()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getviplist()
    },
    async getviplist() {
      const res = await getvip(this.params)
      console.log(res)
      this.viplist = res.data
      this.total = res.count
    },

    hide() {
      this.flag = !this.flag
      this.up = this.flag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
    },
    reload() {
      this.viplist = []
      this.params = {
        sex: '',
        username: '',
        mobile: '',
        page: 1,
        limit: 10
      }
      this.isTableAlive = false
      setTimeout(() => {
        this.isTableAlive = true
      }, 100)
      this.getviplist()
    },
    serach() {
      this.getviplist()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .search-box,
  .tabbar-box {
    padding: 10px 10px;
    height: 50px;
    .filter {
      margin-left: 10px;
    }
  }
  .fontsome {
    margin-left: 15px;
    &:hover {
      border-bottom: 1px solid #51a7ff;
      padding-bottom: 5px;
      color: #51a7ff;
    }
  }
}
.date {
  width: 250px;
}
.status {
  border: 1px solid #d9ecff;
  color: #409eff;
  background-color: #ecf5ff;
  text-align: center;
  border-radius: 5px;
  width: 60px;
  height: 30px;
  font-size: 12px;
  line-height: 30px;
}
.el-pagination {
  text-align: right;
}
.green {
  background-color: #f0f9eb;
  color: #67c23a;
}
.sexswitch {
  margin-top: 7px;
}
</style>
